package cn.qidisoft.edu.hzjt.servlet;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownLoadServlet extends HttpServlet {
  /**
   * 
   */
  private static final long serialVersionUID = 1L;

  /**
   * Constructor of the object.
   */
  public DownLoadServlet() {
    super();
  }

  /**
   * Destruction of the servlet. <br>
   */
  public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
  }

  /**
   * The doGet method of the servlet. <br>
   * 
   * This method is called when a form has its tag value method equals to get.
   * 
   * @param request the request send by the client to the server
   * @param response the response send by the server to the client
   * @throws ServletException if an error occurred
   * @throws IOException if an error occurred
   */
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    doPost(request, response);
  }

  /**
   * The doPost method of the servlet. <br>
   * 
   * This method is called when a form has its tag value method equals to post.
   * 
   * @param request the request send by the client to the server
   * @param response the response send by the server to the client
   * @throws ServletException if an error occurred
   * @throws IOException if an error occurred
   */
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String filename = request.getParameter("filename");
    String chinesefileName = request.getParameter("chinesefileName");
    String dir = request.getParameter("dir");
    // filename = new String(filename.getBytes("iso-8859-1"), "UTF-8");
    // chinesefileName = new String(chinesefileName.getBytes("iso-8859-1"), "UTF-8");
    if (filename != null && filename != "") {
      dir = this.getServletContext().getRealPath(dir) + File.separator + filename;
    }
    File f = new File(dir);
    BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
    byte[] buf = new byte[1024];
    int len = 0;

    response.reset();
    String fname = f.getName();
    if (fname.endsWith(".txt")) {
      fname = new String(fname.getBytes("GBK"), "ISO8859_1");
      response.setContentType("text;charset=GBK");
      response.setHeader("Content-Disposition", "attachment; filename="
          + new String(chinesefileName.getBytes("GBK"), "ISO8859_1") + ".txt");
      // response.setHeader("Content-Disposition", "attachment; filename=" + chinesefileName +
      // ".txt");
    } else if (fname.endsWith(".doc")) {
      fname = new String(fname.getBytes("GBK"), "ISO8859_1");
      response.setContentType("application/doc;charset=GBK");
      response.setHeader("Content-Disposition", "attachment; filename="
          + new String(chinesefileName.getBytes("GBK"), "ISO8859_1") + ".doc");
      // response.setHeader("Content-Disposition", "attachment; filename=" + chinesefileName +
      // ".doc");
    } else if (fname.endsWith(".xls")) {
      fname = new String(fname.getBytes("GBK"), "ISO8859_1");
      response.setContentType("application/ms-excel;charset=UTF-8");
      response.setHeader("Content-Disposition", "attachment; filename="
          + new String(chinesefileName.getBytes("GBK"), "ISO8859_1") + ".xls");
      // response.setHeader("Content-Disposition", "attachment; filename=" + chinesefileName +
      // ".xls");
    } else {
      fname = new String(fname.getBytes("GBK"), "ISO8859_1");
      response.setContentType("application/x-msdownload");
      response.setHeader("Content-Disposition", "attachment; filename=" + chinesefileName);
    }

    OutputStream out = response.getOutputStream();
    while ((len = br.read(buf)) > 0)
      out.write(buf, 0, len);
    br.close();
    out.close();
  }

  /**
   * Initialization of the servlet. <br>
   * 
   * @throws ServletException if an error occure
   */
  public void init() throws ServletException {
    // Put your code here
  }

}
